package phapp.springSecurity;

import javacommon.util.EncodeDecodeUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.encoding.PasswordEncoder;


public class MyPasswordEncoder implements PasswordEncoder
{

   public String encodePassword(String userPassword, Object salt) throws DataAccessException
   {
      String PasswordEncode = "";
      
      try
      {
         PasswordEncode = EncodeDecodeUtil.base64Encode(userPassword);
      }
      catch (Exception eEncode)
      {
         PasswordEncode = "";
         eEncode.printStackTrace();
      }
      
      return PasswordEncode;
   }

   /**
    * 
    * 
    * @param dataPassword
    *           用户数据库中加密的密码
    * @param userPassword
    *           用户请求中的明文的密码
    * @param salt
    *           盐值
    * @return 密码是否正确
    */
   public boolean isPasswordValid(String dataPassword, String userPassword, Object salt) throws DataAccessException
   {
      boolean bResult = false;
      
      System.out.println("InputPasswd[" + userPassword + "] - [" + encodePassword(userPassword, salt) + "]");
      if(userPassword == null || userPassword.trim().length() == 0)
         bResult = false;
      else
         bResult = dataPassword.equals(encodePassword(userPassword, salt));

      return bResult;
   }

}
